//
//  DisplayWaitHandleView.m
//  OASystem
//
//  Created by mac on 14-5-6.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "DisplayWaitHandleView.h"

@interface DisplayWaitHandleView ()

@end

@implementation DisplayWaitHandleView
@synthesize displayHandleDic;
@synthesize workItemGuidStr;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        if(!displayHandleDic)
            displayHandleDic = [[NSDictionary alloc] init];
    }
    return self;
}

//返回上一级
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initNavi
{
    //初始化UINavigation
    self.navigationItem.titleView = [[UIView alloc] initWithFrame:CGRectMake(110, 0, 100, 44)];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    titleLabel.text = @"待办事宜";
    titleLabel.font = HEIGHT_FONT(18.0f);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    [self.navigationItem.titleView addSubview:titleLabel];
    
    //添加导航又边用户信息
    //初始化UINavigation左按钮
    UIButton *helloButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 80, 24)];
    [helloButton setTitle:[NSString stringWithFormat:@"你好，%@",USER_NICK_NAME] forState:UIControlStateNormal];
    helloButton.titleLabel.font = HEIGHT_FONT(13.0f);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:helloButton];
    
    //初始化UINavigation左按钮
    UIButton *naviButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 45, 24)];
    [naviButton setTitle:@"返回" forState:UIControlStateNormal];
    naviButton.titleLabel.font = HEIGHT_FONT(13.0f);
    [naviButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_n.png"] forState:UIControlStateNormal];
    [naviButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_p.png"] forState:UIControlStateHighlighted];
    [naviButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:naviButton];
}

- (void)initView
{
    int offY = 5;
    for (int i = 0; i < titleArray.count; i++)
    {
        CGSize size;
        NSString *str;
        if(i == titleArray.count -2)
            str = [[[displayHandleDic objectForKey:@"attach"] objectAtIndex:0] objectForKey:@"name"];
        else
            str = [displayHandleDic objectForKey:[keyArray objectAtIndex:i]];
        if(str.length != 0)
        {
            size=[HWLabelTextSize getSuitSizeWithString:str fontSize:14 bold:YES sizeOfX:210];
            if(size.height<=16.702000)
                size= CGSizeMake(210, 40);
            else
                size= CGSizeMake(210, 20+size.height/16.702000*20);
        }
        else
            size= CGSizeMake(210, 40);
        


        if(i == titleArray.count -2)
        {
            for (int i = 0; i<[[displayHandleDic objectForKey:@"attach"] count]; i++)
            {
                str = [[[displayHandleDic objectForKey:@"attach"] objectAtIndex:i] objectForKey:@"name"];
                
                int x = str.length/13>=2?80:40;
                
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, offY+x*i, 210, x)];
                label.numberOfLines = 2;
                label.text = str;
                label.font = HEIGHT_FONT(14.0f);
                label.textAlignment = NSTextAlignmentLeft;
                [displayHandleScr addSubview:label];
                
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake(0, 0, 210, x);
                button.tag = i;
                [button addTarget:self action:@selector(getAttack:) forControlEvents:UIControlEventTouchUpInside];
                [label addSubview:button];
                label.userInteractionEnabled = YES;
                offY = offY+x*i;
            }
        }
        else
        {
            UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, offY, 210, size.height)];
            contentLabel.text = str;
            
            if(size.height > 40)
                contentLabel.numberOfLines = 2;//(int)(size.height-20)/20;
            
            
            contentLabel.font = HEIGHT_FONT(14.0f);//[UIFont fontWithName:@"Arial" size:14.0f];
            contentLabel.backgroundColor = (i >= titleArray.count -2)?[UIColor clearColor]:[UIColor whiteColor];
            [displayHandleScr addSubview:contentLabel];
        }
        
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, offY-40*([[displayHandleDic objectForKey:@"attach"] count]-1), 90, size.height)];
        label.text = [titleArray objectAtIndex:i];
        label.font = HEIGHT_FONT(14.0f);
        label.textAlignment = NSTextAlignmentRight;
        [displayHandleScr addSubview:label];
        offY = offY+size.height+10;
        
        if(i == titleArray.count-1)
            label.frame = CGRectMake(10, offY-40, 90, size.height);
        
    }
    displayHandleScr.contentSize = CGSizeMake(320, offY+ 50);
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(60, offY+5, 200, 30);
    [button setTitle:[[[displayHandleDic objectForKey:@"operation"] objectAtIndex:0] objectForKey:@"operationName"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"but.png"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"but_hover.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(hanleAction) forControlEvents:UIControlEventTouchUpInside];
    [displayHandleScr addSubview:button];
}

//获取链接
- (void)getAttack:(UIButton *)button
{
    NSString *attackUrl = [[[displayHandleDic objectForKey:@"attach"] objectAtIndex:button.tag] objectForKey:@"url"];
    NSLog(@"attackUrl-->%@",attackUrl);
    
    if([attackUrl hasPrefix:@"/"]||[attackUrl hasPrefix:@" /"])
        attackUrl = [NSString stringWithFormat:@"%@%@",base_url,attackUrl];
    else
        attackUrl = [NSString stringWithFormat:@"%@/%@",base_url,attackUrl];
    
    attackUrl = [attackUrl  stringByReplacingOccurrencesOfString:@" " withString:@""];
    attackUrl = [attackUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    if(![[UIApplication sharedApplication] openURL:[NSURL URLWithString:attackUrl]])
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TIP" object:@{@"msg":@"获取附件失败"}];
}

//处理请求
- (void)hanleAction
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"LOAD" object:nil];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSString *param2 = [[[displayHandleDic objectForKey:@"operation"] objectAtIndex:0] objectForKey:@"OperationGuid"];
        NSString *param3 = [[[displayHandleDic objectForKey:@"operation"] objectAtIndex:0] objectForKey:@"TransitionGuid"];
        NSLog(@"fhfhf--%@",param3);
        NSDictionary *dic = [[SendHttpOperation shareSendHttpOperation] CommonOperationHandlePass_SingleTransition_Page:TEMP_USERNAME
                                                                                                           WorkItemGuid:workItemGuidStr
                                                                                                          OperationGuid:param2
                                                                                                         TransitionGuid:param3];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"REMOVE" object:nil];
            if ([[dic objectForKey:@"errcode"] isEqualToString:@"0"])
            {
                //push到展示界面
                DealThings *instance = [[DealThings alloc] init];
                instance.dealThingsDic = [dic objectForKey:@"info"];
                instance.workItemGuidStr = workItemGuidStr;
                instance.operationGuidStr = param2;
                instance.transitionGuidStr = param3;
                instance.titleStr = [displayHandleDic objectForKey:@"title"];
                [self.navigationController pushViewController:instance animated:YES];
            }
            else
                [[NSNotificationCenter defaultCenter] postNotificationName:@"TIP" object:@{@"msg":([[dic objectForKey:@"errcode"]intValue]==1?@"获取数据失败":@"用户身份验证失败")}];
        });
    });

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = NOTICEBOARD_BACKCOLOR;
    
    [self initNavi];
    
    if(!titleArray)
        titleArray = [[NSArray alloc] initWithObjects:@"文件标题：",@"主题词：",@"事由：",@"收文序列：",@"来文文号：",@"文件日期：",@"收文日期：",@"密级：",@"份数：",@"页数：",@"排序：",@"归档类目：",@"保存期限：",@"来文单位：",@"附件：",@"备注：", nil];
    if(!keyArray)
        keyArray = [[NSArray alloc] initWithObjects:@"title",@"subject",@"shiYou",@"shouWenNumber",@"laiWenHao",@"fileDate",@"showWenDate",@"miJi",@"copyNum",@"pages",@"orderNumber",@"archiveType",@"baoCunQx",@"laiWenDw",@"attach",@"note", nil];
    
    [self initView];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
